#include<iostream>
#include<cmath>
using namespace std;
const int N = 3e4+10;
int t;
int fa[N];
int d[N];
int n = 3e4;
int cnt[N];
int find(int x)
{
	if (fa[x] == x) return x;
	else
	{
		int t = find(fa[x]);
		d[x] += d[fa[x]];
		return fa[x] = t;
	}
}
void un(int x, int y)
{
	int fx = find(x);int fy = find(y);
	if (fx != fy)
	{
		fa[fx] = fy;
		d[fx] = cnt[fy];
		cnt[fy] += cnt[fx];
		
	}
}
int que(int x, int y)
{
	return abs(d[y]-d[x])-1;//别忘了取绝对值
}
int main()
{
	for (int i = 1;i <= n;i++)
	{
		fa[i] = i;
		cnt[i] = 1;
	}
	cin >> t;
	while (t--)
	{
		char op = 0;
		int x=0, y=0;
		cin >> op;
		cin >> x >> y;

		if (op == 'M')
		{
			//调动
			un(x,y);
		}
		if (op == 'C')
		{
			int fx = find(x);
			int fy = find(y);
			if (fx == fy)
			{
				cout << que(x, y) << endl;
			}
			else cout << -1 << endl;
		}
	}
	return 0;
}